package org.example.test;

import com.alibaba.druid.support.json.JSONUtils;
import org.example.mapper.DeptMapper1;
import org.example.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class test_spring_jdbc {

    @Test
    public void test_jdbc(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-01.xml");
        JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
        //手动封装RowMapper
        String sql = "select * from student where id = ?";
        Student student1 = jdbcTemplate.queryForObject(sql, (rs, rowNum) -> {
                Student student = new Student();
                student.setId(rs.getInt("id"));
                student.setName(rs.getString("name"));
                student.setNo(rs.getString("no"));
                return student;

        }, 1);
        System.out.println(student1);

        //自动封装BeanPropertyRowMapper<>()
        String sql1 = "select * from student";
        List<Student> studentList = jdbcTemplate.query(sql1,new BeanPropertyRowMapper<Student>(Student.class));
        System.out.println(studentList);
    }


}
